package com.surelution.projects.crm

class MarketTrackingController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [marketTrackingInstanceList: MarketTracking.list(params), marketTrackingInstanceTotal: MarketTracking.count()]
    }

    def create = {
        def marketTrackingInstance = new MarketTracking()
        marketTrackingInstance.properties = params
        return [marketTrackingInstance: marketTrackingInstance]
    }
	
	def springSecurityService
	
    def save = {
        def marketTrackingInstance = new MarketTracking(params)
		marketTrackingInstance.handler = User.findByUsername(springSecurityService.principal.username)
        if (marketTrackingInstance.save(flush: true)) {
			if(marketTrackingInstance.preTracking) {
				marketTrackingInstance.preTracking.needFollowup = false
			}
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'marketTracking.label', default: 'MarketTracking'), marketTrackingInstance.id])}"
            redirect(action: "show", id: marketTrackingInstance.market.id, controller:"market")
        }
        else {
            render(view: "create", model: [marketTrackingInstance: marketTrackingInstance])
        }
    }

	def search = {
		def marketInstance = Market.get(params.id)
		def max = Math.min(params.max ? params.int('max') : 10, 100)
		def offset = params.offset ? params.int('offset') : 0
		def marketTrackingInstance = MarketTracking.createCriteria().list([max:max, offset:offset]) {
			eq('market', marketInstance)
			order("handleTime","desc")
		}
		if (!marketTrackingInstance.isEmpty()){
			[marketTrackingInstanceList: marketTrackingInstance, marketTrackingInstanceTotal: marketTrackingInstance.totalCount,market:marketInstance]
		}
		else{
			flash.message = "${message(code: 'default.noTracking.label')}"
			redirect(controller: "market",action: "show",id: marketInstance.id)
		}
	}
	
    def show = {
        def marketTrackingInstance = MarketTracking.get(params.id)
        if (!marketTrackingInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'marketTracking.label', default: 'MarketTracking'), params.id])}"
            redirect(action: "list")
        }
        else {
            [marketTrackingInstance: marketTrackingInstance]
        }
    }

    def edit = {
        def marketTrackingInstance = MarketTracking.get(params.id)
        if (!marketTrackingInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'marketTracking.label', default: 'MarketTracking'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [marketTrackingInstance: marketTrackingInstance]
        }
    }

    def update = {
        def marketTrackingInstance = MarketTracking.get(params.id)
        if (marketTrackingInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (marketTrackingInstance.version > version) {
                    
                    marketTrackingInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'marketTracking.label', default: 'MarketTracking')] as Object[], "Another user has updated this MarketTracking while you were editing")
                    render(view: "edit", model: [marketTrackingInstance: marketTrackingInstance])
                    return
                }
            }
            marketTrackingInstance.properties = params
			def market = marketTrackingInstance.market
			if(market) {
				market = Market.get(market.id)
			} else {
				market = null 
			}
			marketTrackingInstance.market = market
			def handler = marketTrackingInstance.handler
			if(handler) {
				handler = User.get(handler.id)
			} else {
				handler = null
			}
			marketTrackingInstance.handler = handler
			
			def reason = marketTrackingInstance.reason
			if(reason) {
				reason = MarketTrackingReason.get(reason.id)
			} else {
				reason = null
			}
			marketTrackingInstance.reason = reason
			
			def preTracking = marketTrackingInstance.preTracking
			if(preTracking && preTracking.id) {
				preTracking = MarketTracking.get(preTracking.id)
			} else {
				preTracking = null
			}
			marketTrackingInstance.preTracking = preTracking
			
            if (!marketTrackingInstance.hasErrors() && marketTrackingInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'marketTracking.label', default: 'MarketTracking'), marketTrackingInstance.id])}"
				redirect(controller: "market",action: "show",id: marketTrackingInstance.market.id)
            }
            else {
                render(view: "edit", model: [marketTrackingInstance: marketTrackingInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'marketTracking.label', default: 'MarketTracking'), params.id])}"
			redirect(controller: "market",action: "show",id: marketTrackingInstance.market.id)
        }
    }

    def delete = {
        def marketTrackingInstance = MarketTracking.get(params.id)
        if (marketTrackingInstance) {
            try {
                marketTrackingInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'marketTracking.label', default: 'MarketTracking'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'marketTracking.label', default: 'MarketTracking'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'marketTracking.label', default: 'MarketTracking'), params.id])}"
            redirect(action: "list")
        }
    }
}
